package 反射;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class MyReflectDemo2 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        /**
         * 利用反射获取构造方法
         * Class类中用于获取构造方法的方法
         * Constructor<?>[] getConstructors(): 返回所有公共构造方法对象的数组
         * Constructor<?>[] getDeclaredConstructors(): 返回所有构造方法对象的数组
         * Constructor<T> getConstructor(Class<?>... parameterTypes): 返回单个公共构造方法对象
         * Constructor<T>getDeclaredConstructor(Class<?>... parameterTypes): 返回单个构造方法对象
         * Constructor类中用于创建对象的方法
         * TnewInstance(Object...initargs): 根据指定的构造方法创建对象
         * setAccessible(boolean flag): 设置为true,表示取消访问检查
         */
        //1.获取class字节码文件
        Class<?> aClass = Class.forName("反射.students");
        //2.获取构造方法(返回所有公共构造方法对象的数组)
        Constructor<?>[] cons1 = aClass.getConstructors();
        for (Constructor<?> constructor : cons1) {
            System.out.println(constructor);
        }
        System.out.println("================================================");
        //2.获取构造方法(返回所有构造方法对象的数组)
        Constructor<?>[] cons2 = aClass.getDeclaredConstructors();
        for (Constructor<?> constructor : cons2) {
            System.out.println(constructor);
        }
        System.out.println("================================");
        //获取单个的构造方法
        Constructor<?> constructor = aClass.getConstructor();
        System.out.println(constructor);
        System.out.println("================================================");
        //获取单个的构造方法
        constructor = aClass.getDeclaredConstructor();
        System.out.println(constructor);
        System.out.println("================================================");
        //获取单个的构造方法
        constructor = aClass.getConstructor(String.class);
        System.out.println(constructor);
        System.out.println("================================================");
        //获取单个的构造方法
        constructor = aClass.getDeclaredConstructor(int.class);
        System.out.println(constructor);
        System.out.println("================================================");
        //获取单个的构造方法
        Constructor constructor4 = aClass.getDeclaredConstructor(String.class, int.class);
        System.out.println(constructor4);
        System.out.println("================================================");
        //利用反射创建对象
        //暴力反射(强行把权限更改)
        /*constructor4.setAccessible(true);
        student stu = (student) constructor4.newInstance("张三", 14);
        System.out.println(stu);*/
    }
}
class students{
    private String name;
    private int age;

    public students() {
    }
    public students(String name) {
        this.name = name;
    }
    private students(int age){
        this.age=age;
    }
    private students(String name, int age) {
        this.name = name;
        this.age = age;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    public String toString() {
        return "students{name = " + name + ", age = " + age + "}";
    }
}
